// pages/productList/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checkedListNum: 999,
        checkedListInfo: [],
        recommendList: [],
        List: [],
        goodsList: []
    },

    //通用的打印方法，用来查看当前事件传参
    consoleInfo(e) {
        console.log(e)
    },

    // 切换分类
    switchTabList(e) {
        console.log(e)
        const id = parseInt(e.currentTarget.id)
        const type = e.currentTarget.dataset["listtitle"]
        wx.setStorageSync('typeId', id)
        var tabListInfo = []
        if (type === "全部") {
            tabListInfo = this.data.goodsList
        } else {
            this.data.goodsList.forEach(element => {
                if (element.typeId === parseInt(id)) {
                    tabListInfo.push(element)
                }
            });
        }
        this.setData({
            checkedListNum: parseInt(id),
            checkedListInfo: tabListInfo
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const userType = wx.getStorageSync('userType') && wx.getStorageSync('userType') !== "" ? parseInt(wx.getStorageSync('userType')) : 0
        wx.request({
            url: 'https://mbp.jyccloud.cn/mbp/getProductType',
            success: (res) => {
                const all = [{typeId: 999, typeFor: "商品", typeName: "全部"}]
                const list = [...all, ...res.data]
                this.setData({
                    List: list
                })
            }
        })
        wx.request({
            url: 'https://mbp.jyccloud.cn/mbp/getProductList',
            success: (res) => {
                const productList = res.data.map(r => {
                    r["price"] = userType === 1 ? r["referTradePrice"] : r["referPrice"]
                    r["oldPrice"] = r["referPrice"]
                    console.log(r)
                    return r
                })
                this.setData({
                    checkedListInfo: productList,
                    recommendList: productList,
                    goodsList: productList
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this;
        wx.getStorage({
            key: "typeId",
            success(r) {
                const tabListInfo = that.data.goodsList.filter(element => element.typeId === parseInt(r.data));
                that.setData({
                    checkedListNum: parseInt(r.data),
                    checkedListInfo: tabListInfo
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})